<template>
    <h3>用户借款登录</h3>
    <table border="1">
        <tr>
            <td>账号</td>
            <td>
                <input type="text" v-model="info.IUName"/>
            </td>
        </tr>
        <tr>
            <td>密码</td>
            <td>
                <input type="password" v-model="info.IUPwd"/>
            </td>
        </tr>

        <tr>
            <td colspan="2">
                <input type="button" value="登录" @click="LoginInfo()">
            </td>
           
        </tr>
    </table>
</template>

<script setup lang="ts">
import {ref} from 'vue'
import axios from 'axios';
import { useStore } from '../store';


const store=useStore()  

const info=ref({
    IUName:"",
    IUPwd:"",
    IURole:""
})

const LoginInfo=()=>{

    if(info.value.IUName==""||info.value.IUPwd==""){
        alert("账号或密码不能为空")
        return
    }

    axios.get("http://localhost:2134/api/Loan/LoanLogin",{
        params:{
            IUName:info.value.IUName,
            IUPwd:info.value.IUPwd
        }
    })
    .then((res)=>{
        console.log(res.data[0])
        if(res.data.length>0){
            alert("登录成功")
      
            // //存pinia仓库
            // store.IUId=res.data[0].IUId
            console.log(res.data[0].IURole)
            //判断登录用户类型
            ShowType(res.data[0].IURole)
    
        }
        else{
            alert("密码错误")
        }
    })
}

//判断登录用户类型
const ShowType=(IURole:string)=>{
    console.log(IURole)
  
    axios.get("http://localhost:2134/api/Loan/LoginType",{
        params:{
           IURole:IURole
        }
    })
    .then(res=>{
        console.log(res.data[0].IURole)
        if(res.data[0].IURole=="用户"){
            alert("当前为用户页面")
                  location.href="/AddLoan"
        }
        else if(res.data[0].IURole=="管理员"){
            alert("当前为管理员页面")
            location.href="/AdminSolt"
        }
    })
}

</script>